$("header").load("1.head.html")
$("footer").load("2.bottom.html")
class Cart {
   
    //刷新总数
    getGoodsTotalNum() {
        let oGoodsNum = document.getElementsByClassName("goods-num");
        let goodsTotalNum = 0;

        for (let i = 0; i < oGoodsNum.length; i++) {
            goodsTotalNum += oGoodsNum[i].innerHTML / 1;
        }

        let oGoodsTotalNum = document.querySelector(".goods-total-num");
        oGoodsTotalNum.innerHTML = goodsTotalNum;
    }

    //刷新总价格
    getGoodsTotalPrice() {
        let oGoodsSubtotal = document.getElementsByClassName("goods-subtotal");
        let goodsTotalPrice = 0;

        for (let i = 0; i < oGoodsSubtotal.length; i++) {
            goodsTotalPrice += oGoodsSubtotal[i].innerHTML / 1;
        }

        let oGoodsTotalPrice = document.querySelector(".goods-total-price");
        oGoodsTotalPrice.innerHTML = goodsTotalPrice;
    }

    //添加物品
    //需要传递参数,
    //这个参数就是被点击的按钮
    //该方法需要概念四个值
    addGoods(btn) {
        // 1. 数量
        let oGoodsNum = btn.previousElementSibling;
        oGoodsNum.innerHTML = oGoodsNum.innerHTML / 1 + 1;
        // 2. 小计
        // 获取单价
        let oGoodsPrice = btn.parentNode.nextElementSibling.firstElementChild;
        // 获取小计
        let oGoodsSubtotal = btn.parentNode.nextElementSibling.nextElementSibling.firstElementChild;

        oGoodsSubtotal.innerHTML = oGoodsNum.innerHTML * oGoodsPrice.innerHTML;

        // 3. 总数
        this.getGoodsTotalNum();
        // 4. 总价
        this.getGoodsTotalPrice();
        this.updateNumber()
    
    }

    minGoods(btn) {
        let oGoodsNum = btn.nextElementSibling;

        if (oGoodsNum.innerHTML > 0) {
            oGoodsNum.innerHTML = oGoodsNum.innerHTML - 1;

            let oGoodsPrice = btn.parentNode.nextElementSibling.firstElementChild;
            // 获取小计
            let oGoodsSubtotal = btn.parentNode.nextElementSibling.nextElementSibling.firstElementChild;

            oGoodsSubtotal.innerHTML = oGoodsNum.innerHTML * oGoodsPrice.innerHTML;

            // 3. 总数
            this.getGoodsTotalNum();
            // 4. 总价
            this.getGoodsTotalPrice();
            this.updateNumber();
        }
    }


    updateNumber() {
        let params = {
            vipName: '小江',
            goodsId: '001',
            goodsCount: document.querySelector('.goods-num').value
        }
        $.get('./goods/updateGoodsCount.php', params, function(res) {
            console.log('responseText', res)
        })
    }

    //删除或
    delGoods() {
       
        let params = {
            vipName: '小江',
            goodsId: '001',
        }
        let that = this;
        $.get('./goods/deleteGoods.php', params, function(res) {
                // console.log('responseText', res);
                $("#sahnchu").remove();
                // 更新商品总数
                that.getGoodsTotalNum();
                // 更新商品总价格
                that.getGoodsTotalPrice();
            })
            // })
    }

   

    
    //事件绑定
    eventBind() {
        let addbtn = document.querySelector("#add_btn");
        let subbtn = document.querySelector("#sub_btn");
        let delBtn = document.getElementById("del_btn");
        let that = this;
        subbtn.onclick = function() {
            that.minGoods(this);
        };
        addbtn.onclick = function() {
            that.addGoods(this);
        };
        delBtn.onclick = function() {
            that.delGoods(this);
        }
    }

}

let c = new Cart();

c.eventBind();